package me.baicai.modules.belldandy.exception.service.impl;

import me.baicai.exception.domain.GcbException;
import me.baicai.exception.repository.GcbExceptionRepository;
import me.baicai.modules.belldandy.exception.service.GcbExceptionService;
import me.baicai.utils.*;
import me.baicai.modules.belldandy.exception.service.dto.GcbExceptionDTO;
import me.baicai.modules.belldandy.exception.service.dto.GcbExceptionQueryCriteria;
import me.baicai.modules.belldandy.exception.service.mapper.GcbExceptionMapper;
import me.baicai.weixin.application.BasicService;
import me.baicai.weixin.application.Result;
import me.baicai.weixin.entity.gcb.PageEntity;
import me.baicai.weixin.entity.gcb.Query;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author 超级大白菜
 * @date 2019-12-24
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GcbExceptionServiceImpl implements GcbExceptionService
{

    private final GcbExceptionRepository gcbExceptionRepository;

    private final GcbExceptionMapper gcbExceptionMapper;

    public GcbExceptionServiceImpl(GcbExceptionRepository gcbExceptionRepository, GcbExceptionMapper gcbExceptionMapper) {
        this.gcbExceptionRepository = gcbExceptionRepository;
        this.gcbExceptionMapper = gcbExceptionMapper;
    }

    @Override
    public Map<String,Object> queryAll(GcbExceptionQueryCriteria criteria, Pageable pageable){
        Page<GcbException> page = gcbExceptionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gcbExceptionMapper::toDto));
    }

    @Override
    public List<GcbExceptionDTO> queryAll(GcbExceptionQueryCriteria criteria){
        return gcbExceptionMapper.toDto(gcbExceptionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public GcbExceptionDTO findById(Integer id) {
        GcbException gcbException = gcbExceptionRepository.findById(id).orElseGet(GcbException::new);
        ValidationUtil.isNull(gcbException.getId(),"GcbException","id",id);
        return gcbExceptionMapper.toDto(gcbException);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GcbExceptionDTO create(GcbException resources) {
        return gcbExceptionMapper.toDto(gcbExceptionRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GcbException resources) {
        GcbException gcbException = gcbExceptionRepository.findById(resources.getId()).orElseGet(GcbException::new);
        ValidationUtil.isNull( gcbException.getId(),"GcbException","id",resources.getId());
        gcbException.copy(resources);
        gcbExceptionRepository.save(gcbException);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        gcbExceptionRepository.deleteById(id);
    }


    @Override
    public void download(List<GcbExceptionDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GcbExceptionDTO gcbException : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("异常标题", gcbException.getTitle());
            map.put("异常时间", gcbException.getCreated());
            map.put("异常内容", gcbException.getText());
            map.put("异常方法", gcbException.getMethod());
            map.put("异常参数", gcbException.getParameter());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}
